package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.PmsProductAttribute;
import com.itheima.domain.PmsProductAttributeCategory;
import com.itheima.mapper.PmsProductAttributeCategoryMapper;
import com.itheima.mapper.PmsProductAttributeMapper;
import com.itheima.service.PmsProductAttributeCategoryService;
import com.itheima.vo.ProductAttributeVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 产品属性分类表 服务实现类
 * </p>
 *
 * @author zhuguo
 * @since 2022-11-10
 */
@DubboService
public class PmsProductAttributeCategoryServiceImpl extends ServiceImpl<PmsProductAttributeCategoryMapper, PmsProductAttributeCategory> implements PmsProductAttributeCategoryService {

    @Autowired
    private PmsProductAttributeCategoryMapper productAttributeCategoryMapper;
    @Autowired
    private PmsProductAttributeMapper productAttributeMapper;

    // 添加商品属性分类
    @Override
    public void addProductAttributeCategory(String name) {
        PmsProductAttributeCategory productAttributeCategory = new PmsProductAttributeCategory();
        productAttributeCategory.setName(name);
        productAttributeCategoryMapper.insert(productAttributeCategory);
    }

    // 删除单个商品属性分类
    @Override
    public void deleteById(Long attributeId) {
        productAttributeCategoryMapper.deleteById(attributeId);
    }

    // 分页获取所有商品属性分类
    @Override
    public Page<PmsProductAttributeCategory> queryProductAttributeList(Integer pageNum, Integer pageSize) {
        Page<PmsProductAttributeCategory> pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<PmsProductAttributeCategory> wrapper = new LambdaQueryWrapper<>();
        Page<PmsProductAttributeCategory> page = productAttributeCategoryMapper.selectPage(pageInfo, wrapper);
        return page;
    }

    // 获取所有商品属性分类及其下属性
    @Override
    public List<ProductAttributeVo> queryListWithAttr() {
        List<ProductAttributeVo> productAttributeVoList = new ArrayList<>();
        LambdaQueryWrapper<PmsProductAttributeCategory> wrapper = new LambdaQueryWrapper<>();
        List<PmsProductAttributeCategory> attributeCategoryMapperAll = productAttributeCategoryMapper.selectList(wrapper);
        for (PmsProductAttributeCategory productAttributeCategory : attributeCategoryMapperAll) {
            ProductAttributeVo productAttributeVo = new ProductAttributeVo();
            BeanUtils.copyProperties(productAttributeCategory,productAttributeVo);
            LambdaQueryWrapper<PmsProductAttribute> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmsProductAttribute::getProductAttributeCategoryId,productAttributeCategory.getId());
            List<PmsProductAttribute> productAttributeList = productAttributeMapper.selectList(queryWrapper);
            productAttributeVo.setProductAttributeList(productAttributeList);
            productAttributeVoList.add(productAttributeVo);
        }
        return productAttributeVoList;
    }

    // 修改商品属性分类
    @Override
    public void updateAttributeCategory(Long id, String name) {
        PmsProductAttributeCategory productAttributeCategory = new PmsProductAttributeCategory();
        productAttributeCategory.setName(name);
        LambdaQueryWrapper<PmsProductAttributeCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductAttributeCategory::getId,id);
        productAttributeCategoryMapper.update(productAttributeCategory,wrapper);
    }

    // 获取单个商品属性分类信息
    @Override
    public PmsProductAttributeCategory queryAttributeCategoryById(Long id) {
        LambdaQueryWrapper<PmsProductAttributeCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductAttributeCategory::getId,id);
        PmsProductAttributeCategory productAttributeCategory = productAttributeCategoryMapper.selectOne(wrapper);
        return productAttributeCategory;
    }

}
